package cn.wit.summit.chwan.adapters;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.models.db.DownLoadTaskTable;
import cn.wit.summit.chwan.models.dto.GameListBaseBean;
import cn.wit.summit.chwan.models.response.DailyDiscoverResponse;
import cn.wit.summit.chwan.utils.IntentUtil;
import cn.wit.summit.chwan.utils.MyImageLoader;
import cn.wit.summit.chwan.widgets.DownloadViewStrokeBig;
import cn.wit.summit.chwan.widgets.HtmlTextView;

/**
 * Created by zhuguohui on 2016/11/8.
 */

public class RecyclerViewViewPageAdapter extends RecyclerView.Adapter<RecyclerViewViewPageAdapter.MyViewHolder> {

    private Activity context;
    private List<DailyDiscoverResponse> data;

    private Map<String, String> downTemp = new HashMap<>();

    public RecyclerViewViewPageAdapter(Activity context){
        this.context = context;
        this.data = new ArrayList<>();
    }

    public void addData(List<DailyDiscoverResponse> data) {
        this.data.addAll(data);
        notifyDataSetChanged();
    }

    public String getPoByGameId(String gameId) {
        String result = "";
        if (downTemp.size()>0 && downTemp.containsKey(gameId)){
            result = downTemp.get(gameId);
        }
        return result;
    }

    public GameListBaseBean getBeanData(int itemPo) {
        return data.get(itemPo);
    }

    public void setBeanData(int itemPo, DownLoadTaskTable task) {
        data.get(itemPo).setTaskTable(task);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.item_daily_recommend_view, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        DailyDiscoverResponse tempData = data.get(position);
        downTemp.put(tempData.getGame_id(), position+"");
        final DailyDiscoverResponse.GameDiscoverInfoBean temp = tempData.getGame_discover_info();

        holder.textTitleTx.setText(tempData.getGame_name());
        holder.textSubTitleTx.setText(temp.getTitle());
        MyImageLoader.load(holder.imageCoverIv, temp.getPic());
        holder.textDigestHTV.setTextViewHtml(temp.getIntro());
        Format f1 = new SimpleDateFormat("MM月dd日 EEEE");
        String time1 = f1.format(Long.valueOf(temp.getAddtimes()) * 1000);
        holder.dateTagTx.setText(time1);
        holder.installButn.initData(tempData.getGame_id(), tempData);

        holder.boxTitleLl.setOnClickListener(new MyOnClickListener(position));
        holder.imageCoverIv.setOnClickListener(new MyOnClickListener(position));
        holder.textDigestHTV.setOnClickListener(new MyOnClickListener(position));
    }

    public class MyOnClickListener implements View.OnClickListener {
        private int po;

        public MyOnClickListener(int po) {
            this.po = po;
        }

        @Override
        public void onClick(View v) {
            IntentUtil.getInstance().goGamedetial(context,data.get(po).getGame_id());
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        LinearLayout boxTitleLl;
        TextView textTitleTx;
        TextView textSubTitleTx;
        ImageView imageTagIv;
        TextView dateTagTx;
        SimpleDraweeView imageCoverIv;
        HtmlTextView textDigestHTV;
        DownloadViewStrokeBig installButn;

        public MyViewHolder(View itemView) {
            super(itemView);
            boxTitleLl = itemView.findViewById(R.id.boxTitleLl);
            textTitleTx = itemView.findViewById(R.id.textTitleTx);
            textSubTitleTx = itemView.findViewById(R.id.textSubTitleTx);
            imageTagIv = itemView.findViewById(R.id.imageTagIv);
            dateTagTx = itemView.findViewById(R.id.dateTagTx);
            imageCoverIv = itemView.findViewById(R.id.imageCoverIv);
            textDigestHTV = itemView.findViewById(R.id.textDigestHTV);
            installButn = itemView.findViewById(R.id.installButn);
        }
    }
}
